/*
 * Copyright Thoughtworks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.thoughtworks.go.server.database.pg;

import com.thoughtworks.go.server.database.migration.AbstractMigratorIntegrationTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.postgresql.PostgreSQLContainer;

import java.time.Duration;

@Testcontainers
@EnabledOnOs({OS.LINUX, OS.MAC})
class DatabaseMigratorPostgreSQLTest extends AbstractMigratorIntegrationTest {
    @Container
    private final PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer("postgres:18")
        .waitingFor(new WaitAllStrategy()
            .withStrategy(Wait.forLogMessage(".*database system is ready to accept connections.*\\s", 2))
            .withStrategy(Wait.forListeningPort())
            .withStartupTimeout(Duration.ofSeconds(60)));

    @Test
    void shouldMigrate() throws Exception {
        migrate(postgresqlContainer,
                """
                        SELECT
                            table_schema || '.' || table_name
                        FROM
                            information_schema.tables
                        WHERE
                            table_type = 'BASE TABLE'
                        AND
                            table_schema NOT IN ('pg_catalog', 'information_schema');""", postgresqlContainer.getUsername(), postgresqlContainer.getPassword());
    }
}
